﻿using System;
using System.Text;
using System.Windows.Forms;

namespace MyFastDataSourceBind
{
    /// <summary>
    /// 可绑定到数据库字段的列表框控件
    /// </summary>
    /// <remarks>编制 袁永福</remarks>
    public class XDBListBox : System.Windows.Forms.ListBox, IXDataSourceable
    {
        /// <summary>
        /// 初始化对象
        /// </summary>
        public XDBListBox()
        {
        }

        private string strXSystemCodeCategory = null;
        /// <summary>
        /// 系统代码类别
        /// </summary>
        [System.ComponentModel.Editor(
            typeof(XSystemDictionaryNameEditor),
            typeof(System.Drawing.Design.UITypeEditor))]
        public string XSystemCodeCategory
        {
            get { return strXSystemCodeCategory; }
            set { strXSystemCodeCategory = value; }
        }

        /// <summary>
        /// 销毁对象，撤销数据源绑定
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            XDataSourceEventSender.UnRegister(this);
        }

        private string strXDataFieldName = null;
        /// <summary>
        /// 绑定的数据源字段名
        /// </summary>
        [System.ComponentModel.Editor(
            typeof(XDataFieldNameEditor),
            typeof(System.Drawing.Design.UITypeEditor))]
        public string XDataFieldName
        {
            get { return strXDataFieldName; }
            set { strXDataFieldName = value; }
        }

        private string strXDataSourceName = null;
        /// <summary>
        /// 按钮操作的数据源名称
        /// </summary>
        [System.ComponentModel.Editor(
            typeof(XDataSourceNameEditor),
            typeof(System.Drawing.Design.UITypeEditor))]
        public string XDataSourceName
        {
            get
            {
                return strXDataSourceName;
            }
            set
            {
                strXDataSourceName = value;
                XDataSourceEventSender.Register(this, strXDataSourceName);
            }
        }
        /// <summary>
        /// 处理数据源事件
        /// </summary>
        /// <param name="Sender">参数</param>
        /// <param name="Args">参数</param>
        public void HandleDataSourceEvent(object Sender, XDataSourceEventArgs Args)
        {
            this.Enabled = Args.DataSource.Editing;
            switch (Args.Style)
            {
                case XDataSourceEventStyle.FillUI:
                    this.Enabled = Args.DataSource.CanEdit(this.XDataFieldName);
                    string txt = Convert.ToString(Args.DataSource.GetValue(this.XDataFieldName));
                    XSystemDictionary list = XSystemDictionary.GetInstance(this.XSystemCodeCategory);
                    if (list == null)
                    {
                        this.Text = txt;
                    }
                    else
                    {
                        this.Items.Clear();
                        this.Items.AddRange(list.ToArray());
                        this.DisplayMember = "Name";
                        this.ValueMember = "Value";
                        this.SelectedValue = txt;
                    }
                    break;
                case XDataSourceEventStyle.ReadUI:
                    string v = Convert.ToString(this.SelectedValue);
                    if (v == null || v.Length == 0)
                        v = this.Text;
                    Args.DataSource.SetValue(this.XDataFieldName, v);
                    break;
            }
        }
    }//public class XDBListBox
}